import 'dart:async';
import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:flutter_alibc/src/bean/result_bean.dart';
import 'package:flutter_alibc/src/bean/session.dart';

class FlutterAlibc {
  static const MethodChannel _channel = const MethodChannel('flutter_alibc');

  FlutterAlibc._();

  static Future<InitResultInfo> initSdk() async {
    var result = await _channel.invokeMethod('initSdk');
    Map resultMap = json.decode(result);
    return InitResultInfo.fromJson(resultMap);
  }

  static Future<bool> isLogin() async {
    Session session = await getLoginSession();
    return session.userid != null && session.userid.isNotEmpty;
  }

  static Future<Session> login() async {
    String result = await _channel.invokeMethod('login');
    Map resultMap = json.decode(result);
    return Session.fromJson(resultMap);
  }

  static Future<bool> loginout() async {
    bool result = await _channel.invokeMethod('loginout');
    return result;
  }

  static Future<Session> getLoginSession() async {
    String result = await _channel.invokeMethod('getLoginSession');
    Map resultMap = json.decode(result);
    return Session.fromJson(resultMap);
  }

  static Future<String> openByBizCode({
    String pid,
    String adzoneid,
    String taokeAppkey,
    String sellerId,
    String itemId,
    String shopId,
  }) async {
    Map<String, Object> map = {
      "pid": pid,
      "adzoneid": adzoneid,
      "taokeAppkey": taokeAppkey,
      "sellerId": sellerId,
      "itemId": itemId,
      "shopId": shopId,
    };
    String result = await _channel.invokeMethod('openByBizCode', map);
    return result;
  }

  static Future<String> openByUrl({
    String pid,
    String adzoneid,
    String taokeAppkey,
    String sellerId,
    String url,
  }) async {
    Map<String, Object> map = {
      "pid": pid,
      "adzoneid": adzoneid,
      "taokeAppkey": taokeAppkey,
      "sellerId": sellerId,
      "url": url,
    };
    String result = await _channel.invokeMethod('openByUrl', map);
    return result;
  }
}
